package com.niit.users.action;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import com.niit.common.pojo.ApiResponse;
import com.niit.common.pojo.ApiResponseEnum;
import com.niit.common.util.ConstatFinalUtil;
import com.niit.users.pojo.TbMananger;
import com.niit.users.pojo.TbPassenger;
import com.niit.users.service.IUsersDbService;
import com.niit.users.service.impl.UsersDbServiceImpl;

import lombok.extern.log4j.Log4j2;

/**
 * 不需要登录的Action
 */
@Log4j2
public class NoLoginAction {
    private IUsersDbService usersDbService = new UsersDbServiceImpl();
    
    /**
     * 注册(管理员)
     * @param pojo
     * @return
     */
    public ApiResponse<Object> adminsRegister(TbMananger pojo) {
        log.info("==管理员注册==");
        ApiResponse<Object> apiResponse = this.usersDbService.saveOneManangerService(pojo);
        return apiResponse;
    }
    
    /**
     * 登录(管理员)
     * @param pojo
     * @return
     */
    public ApiResponse<Object>  adminsLogin(TbMananger pojo) {
        log.info("==管理员登录==");
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        
        Map<String,Object> paramsMap = new HashMap<String,Object>();
        paramsMap.put("manangerName", pojo.getManangerName());
        ApiResponse<TbMananger> dbApiResponse = this.usersDbService.findOneManangerService(paramsMap);
        TbMananger dbObj = dbApiResponse.getDataOneJava();
        if (Objects.nonNull(dbObj)) {
            if (dbObj.getManagerPasswd().equalsIgnoreCase(pojo.getManagerPasswd())) {
                /* 登录成功 */
                ConstatFinalUtil.SYS_PRO_MAP.put("admins", dbObj);
                apiResponse.setCode(ApiResponseEnum.INFO_LOGIN_SUCCES.getStatus());
            } else {
                /* 密码不正确 */
                apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
            }
        } else {
            /* 账号不存在 */
            apiResponse.setCode(ApiResponseEnum.INFO_ACCOUNT_NOT_EXISTS.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
    
    /**
     * 注册(乘客)
     * @param pojo
     * @return
     */
    public ApiResponse<Object> passRegister(TbPassenger pojo) {
        log.info("==乘客注册==");
        ApiResponse<Object> apiResponse = this.usersDbService.saveOnePassengerService(pojo);
        return apiResponse;
    }
    
    /**
     * 登录(乘客)
     * @param pojo
     * @return
     */
    public ApiResponse<Object> passLogin(TbPassenger pojo) {
        log.info("==乘客登录==");
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        
        Map<String,Object> paramsMap = new HashMap<String,Object>();
        paramsMap.put("passengerIdCardNo", pojo.getPassengerIdCardNo());
        ApiResponse<TbPassenger> dbApiResponse = this.usersDbService.findOnePassengerService(paramsMap);
        TbPassenger dbObj = dbApiResponse.getDataOneJava();
        if (Objects.nonNull(dbObj)) {
            if (dbObj.getPassengerPasswd().equalsIgnoreCase(pojo.getPassengerPasswd())) {
                /* 登录成功 */
                ConstatFinalUtil.SYS_PRO_MAP.put("users", dbObj);
                apiResponse.setCode(ApiResponseEnum.INFO_LOGIN_SUCCES.getStatus());
            } else {
                /* 密码不正确 */
                apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
            }
        } else {
            /* 账号不存在 */
            apiResponse.setCode(ApiResponseEnum.INFO_ACCOUNT_NOT_EXISTS.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
}
